# weather_mcp_sse.py
import random
import uvicorn
from datetime import datetime
from fastmcp import FastMCP
from starlette.applications import Starlette
from starlette.routing import Mount

# 初始化MCP服务
mcp = FastMCP("Weather-SSE-Simulator")

# 模拟天气数据生成
def generate_weather(city: str) -> dict:
    """生成模拟天气数据（不调用真实API）"""
    conditions = ["晴朗", "多云", "小雨", "中雨", "大雪", "雾霾"]
    return {
        "city": city,
        "temperature": round(random.uniform(-5, 35), 1),
        "humidity": random.randint(30, 95),
        "condition": random.choice(conditions),
        "wind_speed": round(random.uniform(1, 20), 1),
        "timestamp": datetime.now().isoformat()
    }

# 注册MCP工具
@mcp.tool()
async def get_weather(city: str) -> str:
    """获取指定城市的天气信息（模拟数据）
    
    参数:
        city: 城市名称，例如 'Beijing'
    """
    weather_data = generate_weather(city)
    return (f"{weather_data['city']}的天气：温度{weather_data['temperature']}℃，"
            f"天气状况：{weather_data['condition']}，"
            f"风速：{weather_data['wind_speed']}m/s")

# 创建应用
app = Starlette(routes=[Mount('/', app=mcp.sse_app())])

if __name__ == "__main__":
    # 启动SSE服务
    uvicorn.run("weather_mcp_sse:app", host="0.0.0.0", port=8002, reload=True)